#!/usr/bin/env python3
"""
项目初始化，设置默认的管理员，公司信息，货币信息，权限信息，角色信息
"""

from pathlib import Path
import sys
from datetime import date
from decimal import Decimal

# 添加项目根目录到Python路径
current_dir = Path(__file__).parent
project_root = current_dir.parent
sys.path.insert(0, str(project_root))

from app import app
from extensions import db
from models import (
    User,
    Role,
    Permission,
    Company,
    Currency,
    user_roles,
    role_permissions,
    user_permissions,
)


def init_database():
    """初始化数据库数据"""
    # app = create_app()

    with app.app_context():
        # 检查数据库文件是否存在，如果不存在则创建表
        instance_path = Path(app.instance_path)
        db_path = instance_path / "data.db"

        if not db_path.exists():
            print("数据库文件不存在，创建新数据库...")
            db.create_all()

        # 初始化数据
        init_company(app)
        init_currencies(app)
        init_permissions(app)
        init_roles(app)
        init_role_permissions(app)
        init_user(app)
        init_user_roles(app)

        print("数据库初始化完成！")


def init_company(app):
    """初始化公司信息"""
    with app.app_context():
        # 检查是否已存在公司数据
        if Company.query.first() is None:
            company_info = {
                "name_cn": "xxx公司",
                "name_en": "xxx Company",
                "address_cn": "xxx地址",
                "address_en": "xxx Address",
                "website": "https://example.com",
            }

            company = Company(**company_info)
            db.session.add(company)
            db.session.commit()
            print("公司信息初始化完成")


def init_currencies(app):
    """初始化货币信息"""
    with app.app_context():
        # 检查是否已存在货币数据
        if Currency.query.first() is None:
            currencies_data = [
                {
                    "code": "CNY",
                    "symbol": "¥",
                    "name_cn": "人民币",
                    "name_en": "Chinese Yuan",
                    "name_local": "人民币",
                    "is_active": True,
                },
                {
                    "code": "USD",
                    "symbol": "$",
                    "name_cn": "美元",
                    "name_en": "US Dollar",
                    "name_local": "dollar",
                    "is_active": True,
                },
                {
                    "code": "EUR",
                    "symbol": "€",
                    "name_cn": "欧元",
                    "name_en": "Euro",
                    "name_local": "euro",
                    "is_active": True,
                },
                {
                    "code": "JPY",
                    "symbol": "¥",
                    "name_cn": "日元",
                    "name_en": "Japanese Yen",
                    "name_local": "円",
                    "is_active": True,
                },
                {
                    "code": "GBP",
                    "symbol": "£",
                    "name_cn": "英镑",
                    "name_en": "British Pound",
                    "name_local": "pound",
                    "is_active": True,
                },
                {
                    "code": "AUD",
                    "symbol": "$",
                    "name_cn": "澳大利亚元",
                    "name_en": "Australian Dollar",
                    "name_local": "dollar",
                    "is_active": True,
                },
                {
                    "code": "CAD",
                    "symbol": "$",
                    "name_cn": "加拿大元",
                    "name_en": "Canadian Dollar",
                    "name_local": "dollar",
                    "is_active": True,
                },
                {
                    "code": "CHF",
                    "symbol": "Fr",
                    "name_cn": "瑞士法郎",
                    "name_en": "Swiss Franc",
                    "name_local": "Franken",
                    "is_active": True,
                },
                {
                    "code": "HKD",
                    "symbol": "$",
                    "name_cn": "港币",
                    "name_en": "Hong Kong Dollar",
                    "name_local": "港幣",
                    "is_active": True,
                },
                {
                    "code": "SGD",
                    "symbol": "$",
                    "name_cn": "新加坡元",
                    "name_en": "Singapore Dollar",
                    "name_local": "dollar",
                    "is_active": True,
                },
                {
                    "code": "RUB",
                    "symbol": "₽",
                    "name_cn": "卢布",
                    "name_en": "Russian Ruble",
                    "name_local": "рубль",
                    "is_active": True,
                },
                {
                    "code": "AED",
                    "symbol": "د.إ",
                    "name_cn": "阿联酋迪拉姆",
                    "name_en": "UAE Dirham",
                    "name_local": "درهم",
                    "is_active": True,
                },
                {
                    "code": "SAR",
                    "symbol": "ر.س",
                    "name_cn": "沙特里亚尔",
                    "name_en": "Saudi Riyal",
                    "name_local": "ريال",
                    "is_active": True,
                },
                {
                    "code": "TRY",
                    "symbol": "₺",
                    "name_cn": "土耳其里拉",
                    "name_en": "Turkish Lira",
                    "name_local": "Türk lirası",
                    "is_active": True,
                },
                {
                    "code": "KRW",
                    "symbol": "₩",
                    "name_cn": "韩国元",
                    "name_en": "South Korean Won",
                    "name_local": "원",
                    "is_active": True,
                },
                {
                    "code": "BRL",
                    "symbol": "R$",
                    "name_cn": "巴西里亚尔",
                    "name_en": "Brazilian Real",
                    "name_local": "Real",
                    "is_active": True,
                },
                {
                    "code": "TWD",
                    "symbol": "NT$",
                    "name_cn": "新台币",
                    "name_en": "New Taiwan Dollar",
                    "name_local": "新台币",
                    "is_active": True,
                },
                {
                    "code": "NZD",
                    "symbol": "$",
                    "name_cn": "新西兰元",
                    "name_en": "New Zealand Dollar",
                    "name_local": "dollar",
                    "is_active": True,
                },
                {
                    "code": "ZAR",
                    "symbol": "R",
                    "name_cn": "南非兰特",
                    "name_en": "South African Rand",
                    "name_local": "rand",
                    "is_active": True,
                },
            ]

            for currency_data in currencies_data:
                currency = Currency(**currency_data)
                db.session.add(currency)

            db.session.commit()
            print("货币信息初始化完成")


def init_permissions(app):
    """初始化权限信息"""
    with app.app_context():
        # 检查是否已存在权限数据
        if Permission.query.first() is None:
            permissions_data = [
                {"codename": "view_user", "name": "查看用户", "content_type": "user"},
                {"codename": "add_user", "name": "添加用户", "content_type": "user"},
                {"codename": "edit_user", "name": "编辑用户", "content_type": "user"},
                {"codename": "delete_user", "name": "删除用户", "content_type": "user"},
                {
                    "codename": "view_product",
                    "name": "查看产品",
                    "content_type": "product",
                },
                {
                    "codename": "add_product",
                    "name": "添加产品",
                    "content_type": "product",
                },
                {
                    "codename": "delete_product",
                    "name": "删除产品",
                    "content_type": "product",
                },
                {
                    "codename": "edit_product",
                    "name": "编辑产品",
                    "content_type": "product",
                },
                {
                    "codename": "add_product_detail",
                    "name": "添加产品详情",
                    "content_type": "product_detail",
                },
                {
                    "codename": "edit_product_detail",
                    "name": "编辑产品详情",
                    "content_type": "product_detail",
                },
                {
                    "codename": "delete_product_detail",
                    "name": "删除产品详情",
                    "content_type": "product_detail",
                },
                {
                    "codename": "download_images",
                    "name": "下载图片",
                    "content_type": "image",
                },
                {
                    "codename": "download_all_images",
                    "name": "批量下载图片",
                    "content_type": "image",
                },
                {
                    "codename": "add_company",
                    "name": "添加公司",
                    "content_type": "company",
                },
                {
                    "codename": "add_salesperson",
                    "name": "添加销售员",
                    "content_type": "user",
                },
                {
                    "codename": "edit_salesperson",
                    "name": "编辑销售员",
                    "content_type": "user",
                },
                {"codename": "admin", "name": "管理权限", "content_type": "permission"},
            ]

            for perm_data in permissions_data:
                permission = Permission(**perm_data)
                db.session.add(permission)

            db.session.commit()
            print("权限信息初始化完成")


def init_roles(app):
    """初始化角色信息"""
    with app.app_context():
        # 检查是否已存在角色数据
        if Role.query.first() is None:
            roles_data = [
                {"name": "超级管理员"},
                {"name": "销售员"},
            ]

            for role_data in roles_data:
                role = Role(**role_data)
                db.session.add(role)

            db.session.commit()
            print("角色信息初始化完成")


def init_role_permissions(app):
    """初始化角色权限关联"""
    with app.app_context():
        # 检查是否已存在角色权限关联
        if db.session.query(role_permissions).first() is None:
            role_perms_data = [
                {"role_id": 2, "permission_id": 1},
                {"role_id": 2, "permission_id": 3},
                {"role_id": 2, "permission_id": 5},
                {"role_id": 2, "permission_id": 6},
                {"role_id": 2, "permission_id": 7},
                {"role_id": 2, "permission_id": 8},
                {"role_id": 2, "permission_id": 9},
                {"role_id": 2, "permission_id": 10},
                {"role_id": 2, "permission_id": 11},
                {"role_id": 2, "permission_id": 12},
                {"role_id": 2, "permission_id": 13},
                {"role_id": 2, "permission_id": 14},
                {"role_id": 2, "permission_id": 16},
                {"role_id": 1, "permission_id": 1},
                {"role_id": 1, "permission_id": 2},
                {"role_id": 1, "permission_id": 3},
                {"role_id": 1, "permission_id": 4},
                {"role_id": 1, "permission_id": 5},
                {"role_id": 1, "permission_id": 6},
                {"role_id": 1, "permission_id": 7},
                {"role_id": 1, "permission_id": 8},
                {"role_id": 1, "permission_id": 9},
                {"role_id": 1, "permission_id": 10},
                {"role_id": 1, "permission_id": 11},
                {"role_id": 1, "permission_id": 12},
                {"role_id": 1, "permission_id": 13},
                {"role_id": 1, "permission_id": 14},
                {"role_id": 1, "permission_id": 15},
                {"role_id": 1, "permission_id": 16},
                {"role_id": 1, "permission_id": 17},
            ]

            # 使用SQLAlchemy的Table对象插入多对多关系数据
            stmt = role_permissions.insert().values(role_perms_data)
            db.session.execute(stmt)
            db.session.commit()
            print("角色权限关联初始化完成")


def init_user(app):
    """初始化用户信息"""
    with app.app_context():
        # 检查是否已存在用户数据
        if User.query.first() is None:
            user_data = {
                "username": "admin",
                "name_cn": "张三",
                "name_en": "Zhang San",
                "phone": "13888888888",
                "email": "admin@example.com",
                "wechat": "wechat",
                "whatsapp": "whatsapp",
                "facebook": "facebook",
                "instagram": "instagram",
                "tiktok": "tiktok",
                "twitter": "twitter",
                "google": "google",
                "gmail": "example@gmail.com",
                "youtube": "youtube",
                "pinterest": "pinterest",
                "is_active": True,
            }

            user = User(**user_data)
            user.set_password("123456")  # 设置密码
            db.session.add(user)
            db.session.commit()
            print("用户信息初始化完成")


def init_user_roles(app):
    """初始化用户角色关联"""
    with app.app_context():
        # 检查是否已存在用户角色关联
        if db.session.query(user_roles).first() is None:
            user_role_data = {"user_id": 1, "role_id": 1}

            # 使用SQLAlchemy的Table对象插入多对多关系数据
            stmt = user_roles.insert().values(user_role_data)
            db.session.execute(stmt)
            db.session.commit()
            print("用户角色关联初始化完成")


if __name__ == "__main__":
    init_database()


# 用户信息字典
# user_info = {
#     "id": 1,
#     "username": "admin",
#     "password": "123456",
#     "name_cn": "张三",
#     "name_en": "Zhang San",
#     "phone": "13888888888",
#     "email": "admin@example.com",
#     "wechat": "wechat",
#     "whatsapp": "whatsapp",
#     "facebook": "facebook",
#     "instagram": "instagram",
#     "tiktok": "tiktok",
#     "twitter": "twitter",
#     "google": "google",
#     "gmail": "example@gmail.com",
#     "youtube": "youtube",
#     "pinterest": "pinterest",
# }


# 角色信息列表
# role_info_list = [
#     {
#         "id": 1,
#         "name": "超级管理员",
#     },
#     {
#         "id": 2,
#         "name": "销售员",
#     },
# ]
# 权限信息列表
# permissions_list = [
#     {"id": 1, "codename": "view_user", "name": "查看用户", "content_type": "user"},
#     {"id": 2, "codename": "add_user", "name": "添加用户", "content_type": "user"},
#     {"id": 3, "codename": "edit_user", "name": "编辑用户", "content_type": "user"},
#     {"id": 4, "codename": "delete_user", "name": "删除用户", "content_type": "user"},
#     {
#         "id": 5,
#         "codename": "view_product",
#         "name": "查看产品",
#         "content_type": "product",
#     },
#     {"id": 6, "codename": "add_product", "name": "添加产品", "content_type": "product"},
#     {
#         "id": 7,
#         "codename": "delete_product",
#         "name": "删除产品",
#         "content_type": "product",
#     },
#     {
#         "id": 8,
#         "codename": "edit_product",
#         "name": "编辑产品",
#         "content_type": "product",
#     },
#     {
#         "id": 9,
#         "codename": "add_product_detail",
#         "name": "添加产品详情",
#         "content_type": "product_detail",
#     },
#     {
#         "id": 10,
#         "codename": "edit_product_detail",
#         "name": "编辑产品详情",
#         "content_type": "product_detail",
#     },
#     {
#         "id": 11,
#         "codename": "delete_product_detail",
#         "name": "删除产品详情",
#         "content_type": "product_detail",
#     },
#     {
#         "id": 12,
#         "codename": "download_images",
#         "name": "下载图片",
#         "content_type": "image",
#     },
#     {
#         "id": 13,
#         "codename": "download_all_images",
#         "name": "批量下载图片",
#         "content_type": "image",
#     },
#     {
#         "id": 14,
#         "codename": "add_company",
#         "name": "添加公司",
#         "content_type": "company",
#     },
#     {
#         "id": 15,
#         "codename": "add_salesperson",
#         "name": "添加销售员",
#         "content_type": "user",
#     },
#     {
#         "id": 16,
#         "codename": "edit_salesperson",
#         "name": "编辑销售员",
#         "content_type": "user",
#     },
#     {"id": 17, "codename": "admin", "name": "管理权限", "content_type": "permission"},
# ]

# 角色权限中间表
# role_permissions_dict_list = [
#     {"role_id": 2, "permission_id": 1},
#     {"role_id": 2, "permission_id": 3},
#     {"role_id": 2, "permission_id": 5},
#     {"role_id": 2, "permission_id": 6},
#     {"role_id": 2, "permission_id": 7},
#     {"role_id": 2, "permission_id": 8},
#     {"role_id": 2, "permission_id": 9},
#     {"role_id": 2, "permission_id": 10},
#     {"role_id": 2, "permission_id": 11},
#     {"role_id": 2, "permission_id": 12},
#     {"role_id": 2, "permission_id": 13},
#     {"role_id": 2, "permission_id": 14},
#     {"role_id": 2, "permission_id": 16},
#     {"role_id": 1, "permission_id": 1},
#     {"role_id": 1, "permission_id": 2},
#     {"role_id": 1, "permission_id": 3},
#     {"role_id": 1, "permission_id": 4},
#     {"role_id": 1, "permission_id": 5},
#     {"role_id": 1, "permission_id": 6},
#     {"role_id": 1, "permission_id": 7},
#     {"role_id": 1, "permission_id": 8},
#     {"role_id": 1, "permission_id": 9},
#     {"role_id": 1, "permission_id": 10},
#     {"role_id": 1, "permission_id": 11},
#     {"role_id": 1, "permission_id": 12},
#     {"role_id": 1, "permission_id": 13},
#     {"role_id": 1, "permission_id": 14},
#     {"role_id": 1, "permission_id": 15},
#     {"role_id": 1, "permission_id": 16},
#     {"role_id": 1, "permission_id": 17},
# ]
#

# 公司信息字典
# 公司信息
# company_info = {
#     "name_cn": "xxx公司",
#     "name_en": "xxx Company",
#     "address_cn": "xxx地址",
#     "address_en": "xxx Address",
#     "website": "https://example.com",
# }

# 货币信息字典
# currencies_dict_list = [
#     {
#         "id": 1,
#         "code": "CNY",
#         "symbol": "¥",
#         "name_cn": "人民币",
#         "name_en": "Chinese Yuan",
#         "name_local": "人民币",
#         "is_active": 1,
#     },
#     {
#         "id": 2,
#         "code": "USD",
#         "symbol": "$",
#         "name_cn": "美元",
#         "name_en": "US Dollar",
#         "name_local": "dollar",
#         "is_active": 1,
#     },
#     {
#         "id": 3,
#         "code": "EUR",
#         "symbol": "€",
#         "name_cn": "欧元",
#         "name_en": "Euro",
#         "name_local": "euro",
#         "is_active": 1,
#     },
#     {
#         "id": 4,
#         "code": "JPY",
#         "symbol": "¥",
#         "name_cn": "日元",
#         "name_en": "Japanese Yen",
#         "name_local": "円",
#         "is_active": 1,
#     },
#     {
#         "id": 5,
#         "code": "GBP",
#         "symbol": "£",
#         "name_cn": "英镑",
#         "name_en": "British Pound",
#         "name_local": "pound",
#         "is_active": 1,
#     },
#     {
#         "id": 6,
#         "code": "AUD",
#         "symbol": "$",
#         "name_cn": "澳大利亚元",
#         "name_en": "Australian Dollar",
#         "name_local": "dollar",
#         "is_active": 1,
#     },
#     {
#         "id": 7,
#         "code": "CAD",
#         "symbol": "$",
#         "name_cn": "加拿大元",
#         "name_en": "Canadian Dollar",
#         "name_local": "dollar",
#         "is_active": 1,
#     },
#     {
#         "id": 8,
#         "code": "CHF",
#         "symbol": "Fr",
#         "name_cn": "瑞士法郎",
#         "name_en": "Swiss Franc",
#         "name_local": "Franken",
#         "is_active": 1,
#     },
#     {
#         "id": 9,
#         "code": "HKD",
#         "symbol": "$",
#         "name_cn": "港币",
#         "name_en": "Hong Kong Dollar",
#         "name_local": "港幣",
#         "is_active": 1,
#     },
#     {
#         "id": 10,
#         "code": "SGD",
#         "symbol": "$",
#         "name_cn": "新加坡元",
#         "name_en": "Singapore Dollar",
#         "name_local": "dollar",
#         "is_active": 1,
#     },
#     {
#         "id": 11,
#         "code": "RUB",
#         "symbol": "₽",
#         "name_cn": "卢布",
#         "name_en": "Russian Ruble",
#         "name_local": "рубль",
#         "is_active": 1,
#     },
#     {
#         "id": 12,
#         "code": "AED",
#         "symbol": "د.إ",
#         "name_cn": "阿联酋迪拉姆",
#         "name_en": "UAE Dirham",
#         "name_local": "درهم",
#         "is_active": 1,
#     },
#     {
#         "id": 13,
#         "code": "SAR",
#         "symbol": "ر.س",
#         "name_cn": "沙特里亚尔",
#         "name_en": "Saudi Riyal",
#         "name_local": "ريال",
#         "is_active": 1,
#     },
#     {
#         "id": 14,
#         "code": "TRY",
#         "symbol": "₺",
#         "name_cn": "土耳其里拉",
#         "name_en": "Turkish Lira",
#         "name_local": "Türk lirası",
#         "is_active": 1,
#     },
#     {
#         "id": 15,
#         "code": "KRW",
#         "symbol": "₩",
#         "name_cn": "韩国元",
#         "name_en": "South Korean Won",
#         "name_local": "원",
#         "is_active": 1,
#     },
#     {
#         "id": 16,
#         "code": "BRL",
#         "symbol": "R$",
#         "name_cn": "巴西里亚尔",
#         "name_en": "Brazilian Real",
#         "name_local": "Real",
#         "is_active": 1,
#     },
#     {
#         "id": 17,
#         "code": "TWD",
#         "symbol": "NT$",
#         "name_cn": "新台币",
#         "name_en": "New Taiwan Dollar",
#         "name_local": "新台币",
#         "is_active": 1,
#     },
#     {
#         "id": 18,
#         "code": "NZD",
#         "symbol": "$",
#         "name_cn": "新西兰元",
#         "name_en": "New Zealand Dollar",
#         "name_local": "dollar",
#         "is_active": 1,
#     },
#     {
#         "id": 19,
#         "code": "ZAR",
#         "symbol": "R",
#         "name_cn": "南非兰特",
#         "name_en": "South African Rand",
#         "name_local": "rand",
#         "is_active": 1,
#     },
# ]
